package com.example.common.base;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;
import com.example.common.utils.LogUtil;
import io.reactivex.rxjava3.disposables.CompositeDisposable;
import io.reactivex.rxjava3.disposables.Disposable;

public class BaseViewModel extends ViewModel {
    
    private CompositeDisposable compositeDisposable = new CompositeDisposable();
    
    public MutableLiveData<Boolean> isLoading = new MutableLiveData<>();
    public MutableLiveData<String> error = new MutableLiveData<>();
    public MutableLiveData<Boolean> isEmpty = new MutableLiveData<>();
    
    public BaseViewModel() {
        LogUtil.d(getClass().getSimpleName(), "BaseViewModel created");
    }
    
    protected void addDisposable(Disposable disposable) {
        compositeDisposable.add(disposable);
    }
    
    protected void showLoading() {
        isLoading.setValue(true);
        error.setValue(null);
        isEmpty.setValue(false);
    }
    
    protected void hideLoading() {
        isLoading.setValue(false);
    }
    
    protected void showError(String errorMsg) {
        isLoading.setValue(false);
        error.setValue(errorMsg);
        isEmpty.setValue(false);
    }
    
    protected void showEmpty() {
        isLoading.setValue(false);
        error.setValue(null);
        isEmpty.setValue(true);
    }
    
    protected void showSuccess() {
        isLoading.setValue(false);
        error.setValue(null);
        isEmpty.setValue(false);
    }
    
    public void retry() {
        LogUtil.d(getClass().getSimpleName(), "retry called");
    }
    
    @Override
    protected void onCleared() {
        super.onCleared();
        LogUtil.d(getClass().getSimpleName(), "onCleared");
        compositeDisposable.clear();
    }
}